package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.CommentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //保存评论
    @Override
    public Integer saveComment(Comment comment) {
        /*
        * 1、保存comment表
        * 2、修改Movement表
        * */
        mongoTemplate.save(comment);

        Integer countResult = 0 ;
        Integer commentType = comment.getCommentType();
        if (commentType == 1){
            Movement movementByLike = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            countResult = movementByLike.getLikeCount() + 1;
            movementByLike.setLikeCount(countResult);
            mongoTemplate.save(movementByLike);
        }else if (commentType == 2){
            Movement movementByComment = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            countResult = movementByComment.getCommentCount() +1 ;
            movementByComment.setCommentCount(countResult);
            mongoTemplate.save(movementByComment);
        }else if (commentType == 3){
            Movement movementByLove = mongoTemplate.findById(comment.getPublishId(), Movement.class);
            countResult = movementByLove.getLoveCount() + 1;
            movementByLove.setCommentCount(countResult);
            mongoTemplate.save(movementByLove);
        }
        return countResult;
    }

    @Override
    public Integer removeComment(Integer commentType, String publishId, Long userId) {
        /*
        * 1、把记录删除
        * 2、查询条数，条数-1，
        * 3、返回数量
        * */

        Query query = new Query(Criteria.where("commentType").is(commentType)
                .and("publishId").is(publishId)
                .and("userId").is(userId));
        mongoTemplate.remove(query,Comment.class);

        Integer countResult = 0;
        if (commentType == 1){
            Movement movementByLike = mongoTemplate.findById(publishId, Movement.class);
            countResult = movementByLike.getLikeCount() - 1;
            movementByLike.setLikeCount(countResult);
            mongoTemplate.save(movementByLike);
        }else if (commentType == 2){
            Movement movementByComment = mongoTemplate.findById(publishId, Movement.class);
            countResult = movementByComment.getCommentCount() - 1;
            movementByComment.setCommentCount(countResult);
            mongoTemplate.save(movementByComment);
        }else if (commentType == 3){
            Movement movementByLove = mongoTemplate.findById(publishId, Movement.class);
            countResult = movementByLove.getLoveCount() - 1;
            movementByLove.setLoveCount(countResult);
            mongoTemplate.save(movementByLove);
        }

        return countResult;
    }

    //查询评论的分页
    @Override
    public PageBeanVo findCommentByPage(String publishId, Integer pageNum, Integer pageSize) {
        Integer index = (pageNum - 1)*pageSize;
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(2))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(index).limit(pageSize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        long count = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(pageNum ,pageSize ,count ,comments);
    }

    //点赞、评论、喜欢数
    @Override
    public PageBeanVo findCommentNum(Integer commentType, Long publishUserId, Integer pageNum, Integer pageSize) {
        Integer index = (pageNum-1)*pageSize;
        Query query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(index).limit(pageSize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        long count = mongoTemplate.count(query, Comment.class);

        return new PageBeanVo(pageNum , pageSize,count ,commentList);
    }
}
